// 建立索引，导入文件
// CONSTRAINT creation
// -------------------
//
// Create node uniqueness constraints, ensuring no duplicates for the given node label and ID property exist in the database. This also ensures no duplicates are introduced in future.
//
// NOTE: The following constraint creation syntax is valid for database version 4.4.0 and above.
CREATE CONSTRAINT `imp_uniq_JobPosition_jobPositionId` IF NOT EXISTS
FOR (n: `JobPosition`)
REQUIRE (n.`jobPositionId`) IS UNIQUE;
CREATE CONSTRAINT `imp_uniq_Company_companyId` IF NOT EXISTS
FOR (n: `Company`)
REQUIRE (n.`companyId`) IS UNIQUE;
CREATE CONSTRAINT `imp_uniq_Education_educationId` IF NOT EXISTS
FOR (n: `Education`)
REQUIRE (n.`educationId`) IS UNIQUE;
CREATE CONSTRAINT `imp_uniq_User_userId` IF NOT EXISTS
FOR (n: `User`)
REQUIRE (n.`userId`) IS UNIQUE;
CREATE CONSTRAINT `imp_uniq_Resume_resumeId` IF NOT EXISTS
FOR (n: `Resume`)
REQUIRE (n.`resumeId`) IS UNIQUE;

:param {
  file_path_root: 'file:///',
  file_0: 'companies.csv',
  file_1: 'jobPositions.csv',
  file_2: 'educations.csv',
  idsToSkip: []
};
// NODE load
// ---------
//
// Load nodes in batches, one node label at a time. Nodes will be created using a MERGE statement to ensure a node with the same label and ID property remains unique. Pre-existing nodes found by a MERGE statement will have their other properties set to the latest values encountered in a load file.
//
// NOTE: Any nodes with IDs in the 'idsToSkip' list parameter will not be loaded.
:auto LOAD CSV WITH HEADERS FROM ($file_path_root + $file_1) AS row
with row
WHERE NOT row.`jobPositionId` IN $idsToSkip AND NOT toInteger(trim(row.`jobPositionId`)) IS NULL
CALL {
  WITH row
  MERGE (n: `JobPosition` { `jobPositionId`: toInteger(trim(row.`jobPositionId`)) })
  SET n.`title` = row.`title`
  SET n.`salary` = row.`salary`
  SET n.`description` = row.`description`
  SET n.`hiring_manager` = row.`hiring_manager`
  SET n.`last_active` = row.`last_active`
  SET n.`link` = row.`link`
} IN TRANSACTIONS OF 10000 ROWS;


:auto LOAD CSV WITH HEADERS FROM ($file_path_root + $file_0) AS row
WITH row
WHERE NOT row.`companyId` IN $idsToSkip AND NOT toInteger(trim(row.`companyId`)) IS NULL
CALL {
  with row
  MERGE (n: `Company` { `companyId`: toInteger(trim(row.`companyId`)) })
  SET n.`companyName` = row.`companyName`
  SET n.`address` = row.`address`
} IN TRANSACTIONS OF 10000 ROWS;

:auto LOAD CSV WITH HEADERS FROM ($file_path_root + $file_2) AS row
WITH row
WHERE NOT row.`educationId` IN $idsToSkip AND NOT toInteger(trim(row.`educationId`)) IS NULL
CALL {
  WITH row
  MERGE (n: `Education` { `educationId`: toInteger(trim(row.`educationId`)) })
  SET n.`educationName` = row.`educationName`
} IN TRANSACTIONS OF 10000 ROWS;


// RELATIONSHIP load
// -----------------
//
// Load relationships in batches, one relationship type at a time. Relationships are created using a MERGE statement, meaning only one relationship of a given type will ever be created between a pair of nodes.
:auto LOAD CSV WITH HEADERS FROM ($file_path_root + $file_1) AS row
WITH row 
CALL {
  WITH row
  MATCH (source: `JobPosition` { `jobPositionId`: toInteger(trim(row.`jobPositionId`)) })
  MATCH (target: `Company` { `companyId`: toInteger(trim(row.`companyId`)) })
  MERGE (source)-[r: `BELONGS_TO`]->(target)
} IN TRANSACTIONS OF 10000 ROWS;

:auto LOAD CSV WITH HEADERS FROM ($file_path_root + $file_1) AS row
WITH row 
CALL {
  WITH row
  MATCH (source: `JobPosition` { `jobPositionId`: toInteger(trim(row.`jobPositionId`)) })
  MATCH (target: `Education` { `educationId`: toInteger(trim(row.`educationId`)) })
  MERGE (source)-[r: `DEGREE_REQUIRED`]->(target)
} IN TRANSACTIONS OF 10000 ROWS;
